#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        perror("参数传输错误！ eg；./mul_copy a.txt 512");
        exit(1);
    }

    int size = atoi(argv[2]);

    char buffer[1024 * size];

    memset(buffer, 0, sizeof(buffer));
    FILE *fp = fopen(argv[1], "r");
    if(!fp)
    {
        printf("打开文件错误！\n");
        exit(1);
    }
    int num = 0;
    int part = 0;

    while(size = fread(buffer, 1, sizeof(buffer), fp))
    {
        part++;
    }
    //这时文件指针已经在文件的末尾。下面再进行文件时读不到文件中字节的，使用fseek将文件指针偏移回文件的开头
    fseek(fp, 0, SEEK_SET);

    char name[20];

    while(size = fread(buffer, 1, sizeof(buffer), fp))
    {
        num++;
        //不能使用char* 因为字符串存储在静态区，只读不能进行修改
        snprintf(name, sizeof(name), "a.txt.%d_%d", num, part);

        FILE* fp1 = fopen(name, "w");

        if(!fp1)
        {
            printf("失败！");
            exit(1);
        }
        
        fwrite(buffer, 1, size, fp1);
        fclose(fp1);
    }
    fclose(fp);
    return 0;
}